package com.fyw.xmcamera;

import android.content.Context;
import android.net.DhcpInfo;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.fyw.xmcamera.util.DeviceWifiManager;
import com.fyw.xmcamera.util.MyUtils;
import com.fyw.xmcamera.util.StringUtils;
import com.lib.FunSDK;
import com.lib.funsdk.support.FunSupport;
import com.lib.funsdk.support.FunWifiPassword;

/**
 * Created by FYW on 2017/10/18.
 */

public class WifiConfigActivity extends BaseActivity {

    private TextView wifiConfigName;
    private EditText wifiConfigPwd;
    private TextView wifiConfigQuickSettingBtn;

    @Override
    protected void handleToolbar(ToolbarHelper toolbarHelper) {
        toolbarHelper.setTitle("快速配置WiFi");
        toolbarHelper.setFinish().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    @Override
    public void initParam(Bundle param) {

    }

    @Override
    public int bindLayout() {
        return R.layout.activity_wifi_config;
    }

    @Override
    public void initView(View view) {
        wifiConfigName = view.findViewById(R.id.wifiConfigName);
        wifiConfigName.setText(getConnectWifiSSID());

        wifiConfigPwd = view.findViewById(R.id.wifiConfigPwd);

        wifiConfigQuickSettingBtn = view.findViewById(R.id.wifiConfigQuickSettingBtn);
    }

    @Override
    public void doBusiness(Context mContext) {
        //快速配置按钮
        wifiConfigQuickSettingBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startQuickSetting();
            }
        });
    }

    private String getConnectWifiSSID() {
        try {
            WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
            return wifiManager.getConnectionInfo().getSSID().replace("\"", "");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    // 开始快速配置
    private void startQuickSetting() {
        try {
            WifiManager wifiManage = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
            WifiInfo wifiInfo = wifiManage.getConnectionInfo();
            DhcpInfo wifiDhcp = wifiManage.getDhcpInfo();

            if (null == wifiInfo) {
                showToast("当前Wifi信息有误");
                return;
            }

            String ssId = wifiInfo.getSSID().replace("\"", "");
            if (StringUtils.isStringNULL(ssId)) {
                showToast("当前Wifi信息有误");
                return;
            }

            ScanResult scanResult = DeviceWifiManager.getInstance(this).getCurScanResult(ssId);
            if (null == scanResult) {
                showToast("当前Wifi信息有误");
                return;
            }

            int pwdType = MyUtils.getEncrypPasswordType(scanResult.capabilities);
            String wifiPwd = wifiConfigPwd.getText().toString().trim();
            if (pwdType != 0 && StringUtils.isStringNULL(wifiPwd)) {
                // 需要密码
                showToast("当前Wifi信息有误");
                return;
            }

            StringBuffer data = new StringBuffer();
            data.append("S:").append(ssId).append("P:").append(wifiPwd).append("T:").append(pwdType);

            String submask;
            if (wifiDhcp.netmask == 0) {
                submask = "255.255.255.0";
            } else {
                submask = MyUtils.formatIpAddress(wifiDhcp.netmask);
            }

            String mac = wifiInfo.getMacAddress();
            StringBuffer info = new StringBuffer();
            info.append("gateway:").append(MyUtils.formatIpAddress(wifiDhcp.gateway)).append(" ip:")
                    .append(MyUtils.formatIpAddress(wifiDhcp.ipAddress)).append(" submask:").append(submask)
                    .append(" dns1:").append(MyUtils.formatIpAddress(wifiDhcp.dns1)).append(" dns2:")
                    .append(MyUtils.formatIpAddress(wifiDhcp.dns2)).append(" mac:").append(mac)
                    .append(" ");

            //TODO
            /*showWaitDialog();*/

            FunSupport.getInstance().startWiFiQuickConfig(ssId,
                    data.toString(), info.toString(),
                    MyUtils.formatIpAddress(wifiDhcp.gateway),
                    pwdType, 0, mac, -1);

            FunWifiPassword.getInstance().saveWifiPassword(ssId, wifiPwd);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
